package org.ohuyo.libra.server.test.msg;

import java.util.Arrays;
import java.util.Collection;

import javax.annotation.Resource;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;
import org.junit.Before;
import org.junit.runners.Parameterized.Parameters;
import org.ohuyo.libra.base.message.service.MasterMessageService;
import org.ohuyo.libra.base.message.service.SlaveMessageService;
import org.ohuyo.libra.server.ticket.service.LoginTicketService;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestContextManager;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

/**
 * 
 * @author rabbit
 * 
 */
@ContextConfiguration(locations = { "classpath:beans.xml" })
public abstract class LibraServerTestBase extends
		AbstractJUnit4SpringContextTests {
	private int mode = 1;

	private final TestContextManager testContextManager;

	private Server server;

	@Resource(name = "org.ohuyo.libra.server.masterMessageService")
	protected MasterMessageService innerMasterMessageService;

	@Resource(name = "org.ohuyo.libra.server.slaveMessageService")
	protected SlaveMessageService innerSlaveMessageService;

	@Resource(name = "org.ohuyo.libra.client.master.masterMessageService")
	protected MasterMessageService rmiMasterMessageService;

	@Resource(name = "org.ohuyo.libra.client.slave.slaveMessageService")
	protected SlaveMessageService rmiSlaveMessageService;

	@Resource(name = "org.ohuyo.libra.client.master.ws.masterMessageService")
	protected MasterMessageService webServiceMasterMessageService;

	@Resource(name = "org.ohuyo.libra.client.slave.ws.slaveMessageService")
	protected SlaveMessageService webServiceSlaveMessageService;
	
	@Resource(name = "org.ohuyo.libra.server.loginTicketService")
	protected LoginTicketService loginTicketService;

	protected MasterMessageService masterMessageService;

	protected SlaveMessageService slaveMessageService;

	public LibraServerTestBase(int mode) {
		this.testContextManager = new TestContextManager(getClass());
		this.mode = mode;
	}

	@Before
	public void injectDependencies() throws Throwable {
		this.testContextManager.prepareTestInstance(this);
		ch(mode);
	}

	@SuppressWarnings("unchecked")
	@Parameters
	public static Collection parms() {
		Object[][] object = { { 1 }, { 2 }, { 3 } };
		return Arrays.asList(object);
	}

	protected void ch(int mode) {
		switch (mode) {
		case 1:
			masterMessageService = innerMasterMessageService;
			slaveMessageService = innerSlaveMessageService;
			break;
		case 2:
			masterMessageService = rmiMasterMessageService;
			slaveMessageService = rmiSlaveMessageService;
			break;
		case 3:
			synchronized (this) {
				if (server == null) {
					Server _server = new Server(8080);
					WebAppContext webapp = new WebAppContext();
					webapp.setContextPath("/");
					webapp.setWar("src/test/resources/jetty/");
					_server.setHandler(webapp);
					try {
						_server.start();
					} catch (Exception e) {
						e.printStackTrace();
					}
					server = _server;
				}
			}
			masterMessageService = webServiceMasterMessageService;
			slaveMessageService = webServiceSlaveMessageService;
			break;
		default:
			masterMessageService = null;
			slaveMessageService = null;
			break;
		}

	}
}
